<?php
 jimport('joomla.application.component.view');
class svViewDemo extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_edit();
				break;
			case 'edit':
				$this->_edit();
				break;
			/*case 'save':
				$this->_save();
				break;*/
			/*case 'cancel':
				$this->_cancel();
				break;*/
			/*case 'remove':
				$this->_remove();
				break;*/
			/*case 'apply':
					$this->_apply();
					break;*/
			default :
				$this->_default();
				$this->getList();
				break;
		}
		parent::display();
	}
	function getList()
	{
		$option[]=array('key'=>0,'text'=>JText::_('select age'));
		$array=$this->get('Parent');
		$array=array_merge($option,$array);
		$this->array=$array;
		$select=JRequest::getVar('age_id');
		$attr='class="inputbox", onchange="submitform()"';
		$list['age']=JHTML::_('select.genericlist',$array,'age_id',$attr,'key','text',$select);
		//--------------------------
		$option1[]=array('key'=>0,'text'=>JText::_('select name'));
		$array1=$this->get('Namelist');
		$array1=array_merge($option1,$array1);
		$select1=JRequest::getVar('name_id');
		$attr='class="inputbox", onchange="submitform()"';
		$list['name']=JHTML::_('select.genericlist',$array1,'name_id',$attr,'key','text',$select1);
		$this->list=$list;
	}
	function _default()
	{
		jimport('joomla.html.pagination');
		$model=$this->getModel('demo');
		// phan trang
		$total=$model->getTotal();
		global $mainframe;
		$param['limitstart']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.limitstart', 'limitstart',0,'int');
		$param['limit']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.limit', 'limit',$mainframe->getCfg('list_limit'),'int');
		$pager=new JPagination($total, $param['limitstart'], $param['limit']);
		// sap xep
		$param['order']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.order', 'filter_order','tk.name','cmd');
		$param['order_Dir']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.orderDir', 'filter_order_Dir','DESC','word');
		// fill du lieu
		$demoList=$model->getAllDemo($param);
		$this->assignRef('pager', $pager);
		$this->assignRef('param', $param);
		$this->assignRef('demoList', $demoList);
		$this->_gridToolbar();
	}
	function _edit()
	{
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$id     = $idlist[0];
		JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
		$demo=JTable::getInstance('demo');
		$demo->load($id);
		$this->assignRef('id', $id);
		$this->assignRef('demo', $demo);
		$this->_formToolbar();
		$this->setLayout('edit');
	}
	function _save()
	{
		$this->AddEdit();
		$this->_default();
	}
	function _apply()
	{
		$id=JRequest::getInt('id');
		$this->AddEdit();
		/*$url="index.php?option=com_svquanlycanbo&view=demo&task=edit&cid[]=".$id;
		$msg="Item saved";
		$this->setRedirect($url,$msg);*/
		header('Location:index.php?option=com_svquanlycanbo&view=demo&task=edit&cid[]='.$id);
	}
	function _cancel()
	{
		/*$url="index.php?option=com_svquanlycanbo&view=demo";
		$msg="All Items";
		$this->setRedirect($url,$msg);*/
		header('Location:index.php?option=com_svquanlycanbo&view=demo');
	}
	function AddEdit()
	{
		JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
		$demo=JTable::getInstance('demo');
		$demo->bind(JRequest::get('post'));
		$demo->store();
	}
	function getPager()
	{
		jimport('joomla.html.pagination');
		$model=$this->getModel('demo');
		$total=$model->getTotal();
		global $mainframe;
		$limitstart=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.limitstart', 'limitstart',0,'int');
		$limit=$mainframe->getUserStateFromRequest('com_svquanlycanbo.demo.limit', 'limit',$mainframe->getCfg('list_limit'),'int');
		$pager=new JPagination($total, $limitstart, $limit);
		$this->assignRef('pager', $pager);
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar()
	{
		JToolBarHelper::title('Quản lý.!');
		JToolBarHelper::save();
		JToolBarHelper::apply();
		JToolBarHelper::cancel();
	}
	
}
?>